package com.winderinfo.yikaotianxia.ui.coupons;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.winderinfo.yikaotianxia.R;
import com.winderinfo.yikaotianxia.core.utils.TimeCompareUtils;
import com.winderinfo.yikaotianxia.core.utils.TimeUtils;
import com.winderinfo.yikaotianxia.core.utils.ToastUtil;
import com.winderinfo.yikaotianxia.ui.order.OrderBean;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


public class CouponAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final int GUOQI = 1;
    private final int WEIGUOQI = 2;

    private Context context;
    private List<CouponsBean.RowsBean> list;

    public CouponAdapter(Context context, List<CouponsBean.RowsBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getItemViewType(int position) {
        String time = TimeUtils.getTime();
        int times = TimeCompareUtils.timeCompare(time, list.get(position).getYkCoupons().getEndtime());
        if (times == GUOQI) {//过期
            return GUOQI;
        } else {//未过期
            return WEIGUOQI;
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        if (i == GUOQI) {//过期
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_coupon_false, viewGroup, false);
            OverdueHolder overdueHolder = new OverdueHolder(view);
            return overdueHolder;
        } else {//未过期
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_coupon_true, viewGroup, false);
            UnexpiredHolder unexpiredHolder = new UnexpiredHolder(view);
            return unexpiredHolder;
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int i) {
        String time = TimeUtils.getTime();
        int times = TimeCompareUtils.timeCompare(time, list.get(i).getYkCoupons().getEndtime());
        if (times == GUOQI) {//过期
            if (viewHolder instanceof OverdueHolder) {
                OverdueHolder overdueHolder = (OverdueHolder) viewHolder;
                overdueHolder.coupf_code.setText("优惠券编号：" + list.get(i).getYkCoupons().getCouponsNo());
                overdueHolder.coupf_endtime.setText("使用截止时间：" + list.get(i).getYkCoupons().getEndtime());
                overdueHolder.coupf_price.setText(list.get(i).getYkCoupons().getMoney()+"");
            }
        }
        if (times == WEIGUOQI) {//未过期
            if (viewHolder instanceof UnexpiredHolder) {
                UnexpiredHolder unexpiredHolder = (UnexpiredHolder) viewHolder;
                unexpiredHolder.coupt_code.setText("优惠券编号：" + list.get(i).getYkCoupons().getCouponsNo());
                unexpiredHolder.coupt_time.setText("使用截止时间：" + list.get(i).getYkCoupons().getEndtime());
                unexpiredHolder.coupt_price.setText(list.get(i).getYkCoupons().getMoney()+"");
            }
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    //过期
    public class OverdueHolder extends RecyclerView.ViewHolder {

        private final TextView coupf_code, coupf_endtime, coupf_price;

        public OverdueHolder(@NonNull View itemView) {
            super(itemView);
            coupf_code = itemView.findViewById(R.id.coupf_code);
            coupf_endtime = itemView.findViewById(R.id.coupf_endtime);
            coupf_price = itemView.findViewById(R.id.coupf_price);
        }
    }

    //没过期
    public class UnexpiredHolder extends RecyclerView.ViewHolder {

        private final TextView coupt_code, coupt_time, coupt_price;

        public UnexpiredHolder(@NonNull View itemView) {
            super(itemView);
            coupt_code = itemView.findViewById(R.id.coupt_code);
            coupt_time = itemView.findViewById(R.id.coupt_time);
            coupt_price = itemView.findViewById(R.id.coupt_price);
        }
    }

}
